using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.Sprites;
using UnityEngine.UI;
using Yoozoo.UI.YGUI;

namespace Yoozoo.Extension
{
    public class TrapezoidImage : YImage
    {
        
        [SerializeField] 
        public int segements;
        [SerializeField] 
        public float fillPercent;
        
        private List<Vector2> _outterVertices = new List<Vector2>();
        protected override void OnPopulateMesh(VertexHelper vh)
        {
            if (CullNoneSprite() || !spriteIsReady(overrideSprite))
            {
                vh.Clear();
                return;
            }
            
            vh.Clear();

            int curSegements = segements + 1;
            float tw = rectTransform.rect.width;
            float th = rectTransform.rect.height;

            float val2 = (float)5 / curSegements;
            float val = 16.5f;
            Vector2 zeroPos = new Vector2(val, 0);
            Vector2 onePos = new Vector2(0, th);
            Vector2 towPos = new Vector2(tw, th);
            Vector2 threePos = new Vector2(tw - val, 0);
            
            float a = tw * fillPercent;
        
            if (a > tw - val)
            {
                vh.Clear();
                float b = 1 - ((tw - a) / val) ;
               
                towPos = new Vector2(a, th);
                for (int i = 0; i <= curSegements; i++)
                {
                    vh.AddVert(new Vector2(zeroPos.x - i*val2, i*val2), color, Vector2.zero);
                }
                vh.AddVert(onePos, color, Vector2.zero);
                vh.AddVert(towPos, color, Vector2.zero);
                vh.AddVert(new Vector2(a, (b*th)), color, Vector2.zero);

                if (val - (tw - a) > 5 )
                {
                    for (int i = curSegements; i >= 0; i--)
                    {
                        vh.AddVert(new Vector2(threePos.x + i*val2, i*val2), color, Vector2.zero);
                    }
                    for (int i = 0 ; i <= curSegements; i++)
                    {
                        vh.AddTriangle(i, i+1, curSegements + 2);
                    }
                    vh.AddTriangle((2*curSegements) + 3 + 1, 0, curSegements + 2);

                    for (int i = (2*curSegements) + 3 ; i > curSegements + 2; i--)
                    {
                        vh.AddTriangle(i+1, i, curSegements + 2);
                    }
                }
                else
                {
                    vh.AddVert(threePos, color, Vector2.zero);
                    for (int i = 0 ; i <= curSegements; i++)
                    {
                        vh.AddTriangle(i, i+1, curSegements + 2);
                    }
                    vh.AddTriangle(curSegements + 3 + 1, 0, curSegements + 2);
                    
                    vh.AddTriangle(curSegements + 3 + 1, curSegements + 3, curSegements + 2);
                }
                
                
                // vh.AddTriangle(i+1, curSegements + 3, curSegements + 2);
            }
            else if (a <= tw - val && a > val)
            {
                vh.Clear();
                towPos = new Vector2(a, th);
                threePos = new Vector2(a, 0);
                for (int i = 0; i <= curSegements; i++)
                {
                    vh.AddVert(new Vector2(zeroPos.x - i*val2, i*val2), color, Vector2.zero);
                }
                vh.AddVert(onePos, color, Vector2.zero);
                vh.AddVert(towPos, color, Vector2.zero);
                vh.AddVert(threePos, color, Vector2.zero);
                for (int i = 0 ; i <= curSegements; i++)
                {
                    vh.AddTriangle(i, i+1, curSegements + 2);
                }
                //
                vh.AddTriangle(curSegements + 3, 0, curSegements + 2);
            }
            else
            {
                vh.Clear();
                float b = a / val;
                float c = b * th;
                zeroPos = new Vector2(a, th - c);
                towPos = new Vector2(a, th);
                vh.AddVert(zeroPos, color, Vector2.zero);
                vh.AddVert(onePos, color, Vector2.zero);
                vh.AddVert(towPos, color, Vector2.zero);
                vh.AddTriangle(0, 1, 2);
            }
            // Vector4 uv = overrideSprite != null ? DataUtility.GetOuterUV(overrideSprite) : Vector4.zero;

            // for (int i = 0; i <= curSegements; i++)
            // {
            //     vh.AddVert(new Vector2(zeroPos.x - i, i), color, Vector2.zero);
            // }
            // vh.AddVert(onePos, color, Vector2.zero);
            // vh.AddVert(towPos, color, Vector2.zero);
            // for (int i = curSegements; i >= 0; i--)
            // {
            //     vh.AddVert(new Vector2(threePos.x + i, i), color, Vector2.zero);
            // }
            //
            // for (int i = 0 ; i <= curSegements; i++)
            // {
            //     vh.AddTriangle(i, i+1, curSegements + 2);
            // }
            // vh.AddTriangle((2*curSegements) + 2 + 1, 0, curSegements + 2);
            // // 4 3 
            // for (int i = (2*curSegements) +2 ; i > curSegements + 2; i--)
            // {
            //     vh.AddTriangle(i+1, i, curSegements + 2);
            // }
        }

        public void LayoutDirty()
        {
            SetAllDirty();
        }
    }
}